package com.woniuxy.stall.web.controller;


import cn.hutool.core.bean.BeanUtil;
import com.woniuxy.core.model.Result;
import com.woniuxy.core.util.JwtUtil;
import com.woniuxy.core.util.RedisUtil;
import com.woniuxy.stall.dao.StallRentTime;
import com.woniuxy.stall.service.StallRentTimeService;
import com.woniuxy.stall.service.param.CerUpStallEveryDayParam;
import com.woniuxy.stall.service.param.RenterUpStallEveryDayParam;
import com.woniuxy.stall.web.controller.form.CerUpStallEveryDayForm;
import com.woniuxy.stall.web.controller.form.RenterUpStallEveryDayForm;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 凌玉春
 * @since 2024年01月15日
 */
@RestController
@RequestMapping("/stallRentTime")
@Api(tags = "出租车位详情接口")
public class StallRentTimeController {
    @Autowired
    private StallRentTimeService stallRentTimeService;
    @Autowired
    private RedisUtil redisUtil;

    @ApiOperation("车位主根据ID设置车位每天出租时间段")
    @PostMapping("/upStallEveryDay")
    public Result upStallEveryDay(@RequestHeader String token, RenterUpStallEveryDayForm form) throws Exception {
        String longToken = redisUtil.get(token);
        Claims claims = JwtUtil.parseUserToken(longToken);
        Integer renterId =(Integer) claims.get("id");
        RenterUpStallEveryDayParam param = BeanUtil.toBean(form, RenterUpStallEveryDayParam.class);
        param.setRenterInfoId(renterId);
        stallRentTimeService.upStallEveryDay(param);
        return Result.getResult().setMessage("设置车位每日出租时间段成功");
    }

    @ApiOperation("物业根据ID设置车位每天出租时间段")
    @PostMapping("/cerUpStallEveryDay")
    public Result cerUpStallEveryDay(@RequestHeader String token, CerUpStallEveryDayForm form) throws Exception {
        String longToken = redisUtil.get(token);
        Claims claims = JwtUtil.parseUserToken(longToken);
        Integer renterId =(Integer) claims.get("id");
        CerUpStallEveryDayParam param = BeanUtil.toBean(form, CerUpStallEveryDayParam.class);
        param.setCertifiedId(renterId);
        stallRentTimeService.cerUpStallEveryDay(param);
        return Result.getResult().setMessage("设置车位每日出租时间段成功");
    }

    @ApiOperation("用户根据车位ID查找出租时间表")
    @GetMapping("/getStallRentTimeByStallId")
    @ApiImplicitParam(name = "stallInfoId",value = "车位信息id")
    public Result getStallRentTimeByStallId(Integer stallInfoId) throws Exception {
        StallRentTime stallRentTime=stallRentTimeService.getStallRentTimeByStallId(stallInfoId);
        return Result.getResult(StallRentTime.class).setMessage("根据车位ID查找出租时间表成功").setData(stallRentTime);
    }

}

